<?php

namespace App\Http\Controllers\Pano;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MakePanoController extends Controller
{
    public function makepano(Request $request, $images)
    {
        //获取输入的图片并组合字符串
//        $images = implode(' ', $this->argument('images'));

        //获取配置文件路径并组合字符串
        $config = $this->getConfig($this->option('config'));

        //获取选项的值并组合字符串
        $option = $this->getOutputPath($this->option('outputpath'));

        //组合系统命令
        $command = public_path() . config('pano.krpanotools') . 'makepano ' . $images . $config . $option;

//        system($command,$return_var);
//        passthru($command,$return_var);

        exec($command, $shell_output, $status);
        print_r($shell_output);

        if ($status == 0) {
            $data = [
                'status' => true,
                'message' => '创建成功'
            ];
        } else {
            $data = [
                'status' => false,
                'message' => '创建失败'
            ];
        }
        return response($data);
    }

    protected function getOutputPath($optionpath)
    {

        $tilepath = '-tilepath="' . public_path() . config('pano.outputpath') . $optionpath . config('pano.tilepath') . '" ';       //图片的保存路径

        $thumbpath = '-thumbpath="' . public_path() . config('pano.outputpath') . $optionpath . config('pano.thumbpath') . '" ';      //缩略图的保存路径

        $previewpath = '-previewpath="' . public_path() . config('pano.outputpath') . $optionpath . config('pano.previewpath') . '" ';      //缩略图的保存路径

        $customimage = '-"customimage[vr].path"="' . public_path() . config('pano.outputpath') . $optionpath . config('pano.customimage') . '" ';

        $xmlpath = '-xmlpath="' . public_path() . config('pano.outputpath') . $optionpath . config('pano.xmlpath') . '" ';      //缩略图的保存路径

        $options = $tilepath . $thumbpath . $previewpath . $customimage . $xmlpath;

        return $options;
    }

    protected function getConfig($optionconfig)
    {
        if ($optionconfig == 0) {
//           自定义漫游模式
            $config = '-config="' . public_path() . config('pano.customconfig') . '" ';

        } elseif ($optionconfig == 1) {
//            1为普通漫游模式
            $config = '-config="' . public_path() . config('pano.normalconfig') . '" ';
        } else {
//            0为全景漫游模式
            $config = '-config="' . public_path() . config('pano.vtourconfig') . '" ';
        }
        return ' ' . $config;
    }
}
